import getopt, sys
import csv
import tagger
class Config:
    skipHeader = True
    
config = Config()



def usage():
    print('''
tagTLN.py [-s] <tln_csv_file>
    -s: the csv file has headers
    ''')
    sys.exit(1)
def parse_args():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "o:hs", ["help", "output="])
    except getopt.GetoptError as err:
        # print help information and exit:
        print (str(err)) # will print something like "option -a not recognized"
        usage()
        sys.exit(2)
    output = None
    verbose = False
    if (len(args) == 0):
        usage()
    else :
        config.input = args[0]
    for o, a in opts:
        if o == '-s':
            config.skipHeader = True
        elif o in ("-h", "--help"):
            usage()
        elif o in ("-o", "--output"):
            config.output = a
        else:
            assert False, "unhandled option"
def process():
    csvReader = csv.reader(open(config.input, 'r'))
    csvWriter = csv.writer(sys.stdout)
    csvWriter.writerow(['time', 'src' , 'host', 'usr' ,
                        'action' , 'target' , 'misc' ,
                        'tamper' , 'tag'])
    for row in csvReader:
        if config.skipHeader == True:
            config.skipHeader = False
            continue
        csvWriter.writerow(row + [tagger.calculatetag(row)])
    
if __name__ == "__main__":
    parse_args()
    process()
